package cn.wsalix.biz.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.form.UploadFileForm;
import cn.anlaser.service.impl.SimpleServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.biz.entity.UploadFile;
import cn.wsalix.biz.form.FreemarkerForm;
import cn.wsalix.biz.repository.UploadFileRepository;
import cn.wsalix.biz.service.AdvertiseService;
import cn.wsalix.biz.service.ArticleService;
import cn.wsalix.biz.service.CategoryService;
import cn.wsalix.biz.service.UploadFileService;
import cn.wsalix.freemarker.FreemarkerUtil;
import cn.wsalix.observable.service.FileUploadService;
import cn.wsalix.observable.service.GenerateService;
import cn.wsalix.shop.service.BrandService;
import cn.wsalix.shop.service.OriginService;
import cn.wsalix.shop.service.ProductService;
import cn.wsalix.user.service.UserInfoService;

@Service("uploadFileService")
public class UploadFileServiceImpl extends
		SimpleServiceImpl<UploadFileForm, UploadFileRepository, UploadFile>
		implements UploadFileService {
	@Autowired
	private UploadFileRepository uploadFileRepository;
	@Autowired
	private FreemarkerUtil freemarkerUtil;

	@Override
	public String module() {
		return "/biz/upload_file";
	}

	@Override
	public UploadFileRepository getRepository() {
		return uploadFileRepository;
	}

	@Override
	public Class<UploadFile> getEntityClazz() {
		return UploadFile.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(UploadFileForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		/*
		 * if (form.getProductId() != null && form.getProductId() != 0) {
		 * filters.put("product.id", new SearchFilter("product.id", Operator.EQ,
		 * form.getProductId())); } if (form.getAdId() != null && form.getAdId()
		 * != 0) { filters.put("ad.id", new SearchFilter("ad.id", Operator.EQ,
		 * form.getAdId())); }
		 */
		if (form.getFkId() != null && form.getFkId() != 0) {
			filters.put("fkId",
					new SearchFilter("fkId", Operator.EQ, form.getFkId()));
		}
		if (form.getType() != null) {
			filters.put("type",
					new SearchFilter("type", Operator.EQ, form.getType()));
		}

		return filters;
	}

	/*
	 * @Override public List<UploadFile> findByProductId(Long productId) {
	 * UploadFileForm uploadFileForm = new UploadFileForm();
	 * uploadFileForm.setFkId(productId);
	 * uploadFileForm.setType(FileTypeEnum.product);
	 * QuerySpecification<UploadFile> spec = DynamicSpecifications
	 * .bySearchFilter(buildByForm(uploadFileForm).values(), getEntityClazz());
	 * return uploadFileRepository.findAll(spec); }
	 */
	/*
	 * @Override public List<UploadFile> findByAdId(Long adId) { UploadFileForm
	 * uploadFileForm = new UploadFileForm(); uploadFileForm.setFkId(adId);
	 * uploadFileForm.setType(FileTypeEnum.ad); QuerySpecification<UploadFile>
	 * spec = DynamicSpecifications
	 * .bySearchFilter(buildByForm(uploadFileForm).values(), getEntityClazz());
	 * return uploadFileRepository.findAll(spec); }
	 */
	@Transactional
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof FileUploadService) {
			FileUploadService fileUploadService = (FileUploadService) o;
			if (arg instanceof UploadFileForm) {
				UploadFileForm obj = (UploadFileForm) arg;
				super.add(obj);
			}

		}
		if (o instanceof GenerateService) {
			GenerateService service = (GenerateService) o;
			FreemarkerForm form = (FreemarkerForm) arg;
			freemarkerUtil.buildFile(form);
		}
	}

	@Autowired
	private ArticleService articleService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private AdvertiseService advertiseService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ProductService productService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private OriginService originService;
	/*
	 * @Override public Object findFkByForm(UploadFileForm form) {
	 * switch(form.getType()){ case ad: return
	 * advertiseService.findById(form.getFkId()); case article: return
	 * articleService.findById(form.getFkId()); case avatar: return
	 * userInfoService.findById(form.getFkId()); case brand: return
	 * brandService.findById(form.getFkId()); case category: return
	 * categoryService.findById(form.getFkId()); case origin: return
	 * originService.findById(form.getFkId()); case product: return
	 * productService.findById(form.getFkId()); case unknown: break; default:
	 * break; } return null; }
	 */
}
